<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>websocket_TEST</title>
</head>
<body>
<!--<textarea class="logo" style="width: 100%; height: 500px;">-->
<!--=======websocket======-->
<!--</textarea>-->
<input type="button" value="连接" onClick="link()">
<input type="button" value="断开" onClick="dis()">
<input type="text" value="" id="sendText">
<input type="button" value="发送" onClick="send()" id="sendBtn">

<!--<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>-->
<script>
    function link()  {
        var url = "ws://192.168.8.66:8000";
        //兼容 FireFox
        if ("WebSocket" in window)  {
            socket = new WebSocket(url);
        } else if("MozWebSocket" in window)  {
            socket = new MozWebSocket(url);
        }
        socket.onopen = function(event)  {
            console.log('连接成功');
            alert('连接成功');
        };
        socket.onerror = function(evt)  {
            console.log("WebSocketError!");
            alert('WebSocketError');
        };
    }

    function dis() {
        socket.onclose=function(evt)  {
            console.log('断开连接');
            alert('断开连接');
        };
        socket.close();
        socket = null;
    }

    function send()  {
        var content = document.getElementById("sendText").value;
        console.log(content);
        socket.send(content);
        alert('发送完成');
        socket.onmessage=function(event)  {
            var content = event.data;
            console.log(content);
            if(content.length > 0)  {
                alert("server said:" + content);
            }
        };
    }
</script>

</body>
</html>
